#common makefile header

# 设置目标架构，默认为arm
ARCH ?= arm

# 设置交叉编译工具链路径
TOOLCHAIN_DIR ?= /opt/atk-dlrv1126-toolchain
SYSROOT ?= $(TOOLCHAIN_DIR)/arm-buildroot-linux-gnueabihf/sysroot

# 设置目录和工具链
DIR_INC = ./include:./ivw_record_sample
DIR_BIN = ./bin
DIR_LIB = ./libs

# 设置交叉编译工具链
CROSS_COMPILE = $(TOOLCHAIN_DIR)/usr/bin/arm-linux-gnueabihf-

# 添加sysroot支持
SYSROOT_CFLAGS = --sysroot=$(SYSROOT)

# 定义所有目标
TTS_TARGET = xtts_sample
IVW_TARGET = ivw_sample

TTS_BIN_TARGET = $(DIR_BIN)/$(TTS_TARGET)
IVW_BIN_TARGET = $(DIR_BIN)/$(IVW_TARGET)

# 基础编译选项
BASE_CFLAGS = -g -Wall -I$(DIR_INC) -I. $(SYSROOT_CFLAGS)
BASE_CPPFLAGS = -std=c++11 $(BASE_CFLAGS)

# 应用特定选项
CFLAGS = $(BASE_CFLAGS) -Wno-format -Wno-unused-function
CPPFLAGS = $(BASE_CPPFLAGS)
LDFLAGS = -L$(DIR_LIB) $(SYSROOT_CFLAGS) -laikit -lrt -ldl -lpthread -lasound

# 源文件定义
TTS_SOURCES = xtts_samle.cpp tts_interface.cpp simple_kws.cpp ivw_record_sample/speech_recognizer.cpp ivw_record_sample/linuxrec.c
IVW_SOURCES = ivw_record_sample/ivw_record_sample.cpp ivw_record_sample/speech_recognizer.cpp ivw_record_sample/linuxrec.c

# 对象文件
TTS_OBJECTS = xtts_samle.o tts_interface.o simple_kws.o ivw_record_sample/speech_recognizer.o ivw_record_sample/linuxrec.o
IVW_OBJECTS = ivw_record_sample/ivw_record_sample.o ivw_record_sample/speech_recognizer.o ivw_record_sample/linuxrec.o

# Qt specific flags
#QT_CFLAGS = $(shell pkg-config --cflags Qt5Core Qt5Gui)
#QT_LDFLAGS = $(shell pkg-config --libs Qt5Core Qt5Gui)

# Source files for IvwQtInterface demo
#IVW_QT_SOURCES = IvwQtInterface.cpp simple_kws.cpp main_qt_ivw.cpp
#IVW_QT_OBJECTS = $(patsubst %.cpp,%.o,$(IVW_QT_SOURCES))
IVW_QT_TARGET = $(DIR_BIN)/ivw_qt_demo

# Default target: build all executables
all: $(TTS_BIN_TARGET) $(IVW_BIN_TARGET)

# TTS示例程序
$(TTS_BIN_TARGET): $(TTS_OBJECTS) | $(DIR_BIN)
	$(CROSS_COMPILE)g++ $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

# IVW示例程序
$(IVW_BIN_TARGET): $(IVW_OBJECTS) | $(DIR_BIN)
	$(CROSS_COMPILE)g++ $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

# # IVW Qt Demo Application
#$(IVW_QT_TARGET): $(IVW_QT_OBJECTS) | $(DIR_BIN)
#	$(CROSS_COMPILE)g++ $(CPPFLAGS) $(QT_CFLAGS) $^ -o $@ $(LDFLAGS) $(QT_LDFLAGS)

# 创建bin目录
$(DIR_BIN):
	mkdir -p $(DIR_BIN)

%.o: %.c
	$(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@

%.o: %.cpp
	$(CROSS_COMPILE)g++ -c $(CPPFLAGS) $< -o $@

# 清理
clean:
	@rm -f *.o ivw_record_sample/*.o $(TTS_BIN_TARGET) $(IVW_BIN_TARGET) $(IVW_QT_OBJECTS) $(IVW_QT_TARGET)
	@rm -rf $(DIR_BIN)

# 单独构建目标
tts: $(TTS_BIN_TARGET)
ivw: $(IVW_BIN_TARGET)

.PHONY: all clean tts ivw

#common makefile foot